<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="手环imei号" prop="kidbandImei">
        <el-input v-model="formData.kidbandImei" placeholder="请输入手环imei号" />
      </el-form-item>
      <el-form-item label="序列号" prop="imei">
        <el-input v-model="formData.imei" placeholder="请输入序列号" />
      </el-form-item>
      <el-form-item label="MAC地址" prop="macCode">
        <el-input v-model="formData.macCode" placeholder="请输入MAC地址" />
      </el-form-item>
      <el-form-item label="主标识" prop="majorCode">
        <el-input v-model="formData.majorCode" placeholder="请输入主标识" />
      </el-form-item>
      <el-form-item label="子标识" prop="minorCode">
        <el-input v-model="formData.minorCode" placeholder="请输入子标识" />
      </el-form-item>
      <el-form-item label="所属园区" prop="schoolCode">
        <el-select v-model="formData.schoolCode" placeholder="请选择所属园区">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_CODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="班级ID" prop="classId">
        <el-input v-model="formData.classId" placeholder="请输入班级ID" />
      </el-form-item>
      <el-form-item label="学生ID" prop="studentId">
        <el-input v-model="formData.studentId" placeholder="请输入学生ID" />
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input v-model="formData.studentName" placeholder="请输入学生姓名" />
      </el-form-item>
      <el-form-item label="信标位置名称" prop="locationName">
        <el-input v-model="formData.locationName" placeholder="请输入信标位置名称" />
      </el-form-item>
      <el-form-item label="位置类型" prop="locationType">
        <el-select v-model="formData.locationType" placeholder="请选择位置类型">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_BEACON_LOCATION_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="定位时间" prop="locationTime">
        <el-date-picker
          v-model="formData.locationTime"
          type="date"
          value-format="x"
          placeholder="选择定位时间"
        />
      </el-form-item>
      <el-form-item label="信号强度值" prop="rssi">
        <el-input v-model="formData.rssi" placeholder="请输入信号强度值" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="formData.sort" placeholder="请输入排序" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { DeviceBeaconDataApi, DeviceBeaconData } from '@/api/school/smartDevice/beaconData'

/** 信标定位数据表 表单 */
defineOptions({ name: 'DeviceBeaconDataForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  kidbandImei: undefined,
  imei: undefined,
  macCode: undefined,
  majorCode: undefined,
  minorCode: undefined,
  schoolCode: undefined,
  classId: undefined,
  studentId: undefined,
  studentName: undefined,
  locationName: undefined,
  locationType: undefined,
  locationTime: undefined,
  rssi: undefined,
  sort: undefined,
  status: undefined
})
const formRules = reactive({
  imei: [{ required: true, message: '序列号不能为空', trigger: 'blur' }],
  macCode: [{ required: true, message: 'MAC地址不能为空', trigger: 'blur' }],
  majorCode: [{ required: true, message: '主标识不能为空', trigger: 'blur' }],
  minorCode: [{ required: true, message: '子标识不能为空', trigger: 'blur' }],
  schoolCode: [{ required: true, message: '所属园区不能为空', trigger: 'change' }],
  studentId: [{ required: true, message: '学生ID不能为空', trigger: 'blur' }],
  locationName: [{ required: true, message: '信标位置名称不能为空', trigger: 'blur' }],
  locationType: [{ required: true, message: '位置类型不能为空', trigger: 'change' }],
  locationTime: [{ required: true, message: '定位时间不能为空', trigger: 'blur' }],
  sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await DeviceBeaconDataApi.getDeviceBeaconData(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as DeviceBeaconData
    if (formType.value === 'create') {
      await DeviceBeaconDataApi.createDeviceBeaconData(data)
      message.success(t('common.createSuccess'))
    } else {
      await DeviceBeaconDataApi.updateDeviceBeaconData(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    kidbandImei: undefined,
    imei: undefined,
    macCode: undefined,
    majorCode: undefined,
    minorCode: undefined,
    schoolCode: undefined,
    classId: undefined,
    studentId: undefined,
    studentName: undefined,
    locationName: undefined,
    locationType: undefined,
    locationTime: undefined,
    rssi: undefined,
    sort: undefined,
    status: undefined
  }
  formRef.value?.resetFields()
}
</script>
